from PyQt5 import QtWidgets,QtCore
from pkg.voice import out
from pkg.db import db


class NewUserClass(QtWidgets.QDialog):
    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint)
        self.id_label=QtWidgets.QLabel('用户名：')
        self.id_input=QtWidgets.QLineEdit()
        self.nick_label=QtWidgets.QLabel('昵称：')
        self.nick_input=QtWidgets.QLineEdit()
        self.sex_label=QtWidgets.QLabel('性别：')
        self.sex_comb=QtWidgets.QComboBox()
        self.sex_comb.insertItem(0,'男')
        self.sex_comb.insertItem(1,'女')
        self.birth_label=QtWidgets.QLabel('生日：')
        self.birth_date=QtWidgets.QCalendarWidget()
        self.ok_btn=QtWidgets.QPushButton('完成')
        self.cancel_btn = QtWidgets.QPushButton('取消')
        self.top_layout=QtWidgets.QGridLayout()
        self.top_layout.addWidget(self.id_label,0,0)
        self.top_layout.addWidget(self.id_input,0,1)
        self.top_layout.addWidget(self.nick_label,1,0)
        self.top_layout.addWidget(self.nick_input,1,1)
        self.top_layout.addWidget(self.sex_label,2,0)
        self.top_layout.addWidget(self.sex_comb,2,1)
        self.top_layout.addWidget(self.birth_label,3,0)
        self.top_layout.addWidget(self.birth_date,3,1)
        self.bottom_layout=QtWidgets.QHBoxLayout()
        self.bottom_layout.addWidget(self.ok_btn)
        self.bottom_layout.addWidget(self.cancel_btn)
        self.main_layout=QtWidgets.QVBoxLayout()
        self.main_layout.addLayout(self.top_layout)
        self.main_layout.addLayout(self.bottom_layout)
        self.setLayout(self.main_layout)
        self.ok_btn.clicked.connect(self.on_ok_clicked)
        self.cancel_btn.clicked.connect(self.on_cancel_clicked)

    def on_ok_clicked(self):
        self.accept()

    def on_cancel_clicked(self):
        self.done(0)

    def accept(self):
        if self.check_data():
            self.done(1)

    def check_data(self):
        name=self.id_input.text()
        if len(name)==0:
            out.output_thread('姓名不能为空，请重新输入')
            return False
        if len(name)>32:
            out.output_thread('姓名不能超过32个字符，请重新输入')
            return False
        if db.is_user_exist(name):
            out.output_thread('用户已存在，请重新输入')
            return False
        nick = self.nick_input.text()
        if len(nick) == 0:
            out.output_thread('昵称不能为空，请重新输入')
            return False
        if len(nick) > 32:
            out.output_thread('昵称不能超过32个字符，请重新输入')
            return False
        return True
